package com.ag.admin.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.ag.admin.service.IAuthorizeService;
import com.ag.admin.utils.UserSession;
import com.ag.admin.utils.UserUtil;
import com.ag.bean.TbEvent;
import com.ag.bean.TbPermission;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserRole;
import com.ag.common.EventUtil;
import com.ag.lzm.dao.BaseDAO;
import com.ag.lzm.dao.TbEventDAO;

/**
 * 用户授权验证服务类
 * 
 * @author 黎肇明
 * 
 */
public class AuthorizeServiceImpl implements IAuthorizeService {
	@Resource(name = "LoginOutServiceImpl")
	private LoginOutServiceImpl loginOutService;

	@Resource(name = "LZMBaseDAO")
	private BaseDAO baseDAO;
	@Resource(name = "LZMTbEventDAO")
	private TbEventDAO eventDAO;

	/**
	 * 根据标签权限码及用户Session验证权限
	 */
	@Override
	public boolean checkCompetence(HttpSession session, String permission) {
		UserSession userSession = (UserSession) session
				.getAttribute(UserUtil.USER_SESSION);
		if (userSession != null && !permission.isEmpty()) {
			Map<String, Map<String, TbPermission>> roleMap = userSession
					.getUserPemission();
			for (String roleMapKey : roleMap.keySet()) {
				Map<String, TbPermission> authMap = roleMap.get(roleMapKey);
				if (authMap.containsKey(permission)) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean checkRole(HttpSession session, String roleID) {
		UserSession userSession = (UserSession) session
				.getAttribute(UserUtil.USER_SESSION);
		List<TbUserRole> roleList = loginOutService
				.findRoleEventByUserID(userSession.getTbUser().getUId());
		for (TbUserRole tbUserRole : roleList) {
			if (roleID.equals(tbUserRole.getTbRole().getRId())) {
//				System.out.println("test1"+tbUserRole.getTbEvent()==null);
//				System.out.println("test2"+tbUserRole.getTbEvent().getEId());
//				System.out.println("test3"+(String) session
//						.getAttribute(EventUtil.PROCESSING_EVENT_ID));
				if (tbUserRole.getTbEvent()!=null) {
					if (tbUserRole.getTbEvent().getEId().equals(
							(String) session
									.getAttribute(EventUtil.PROCESSING_EVENT_ID))) {
						return true;
					}
				}
				
			}
		}
		return false;
	}

	/**
	 * 符合角色则返回TRUE，否则返回FALSE
	 */
	@Override
	public boolean checkRoleGen(HttpSession session, String roleID) {
		UserSession userSession = (UserSession) session
				.getAttribute(UserUtil.USER_SESSION);
		List<TbUserRole> roleList = loginOutService
				.findRoleEventByUserID(userSession.getTbUser().getUId());
		for (TbUserRole tbUserRole : roleList) {
			if (roleID.equals(tbUserRole.getTbRole().getRId())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 如果没有参与过呈批或呈批经过上级同意，则返回TRUE
	 */
	@Override
	public boolean checkSubmitDisposition(HttpSession session, String eventID) {
		UserSession userSession = (UserSession) session
				.getAttribute(UserUtil.USER_SESSION);
		TbUser user = userSession.getTbUser();
		Object[] obj = { eventID, user.getUId() };
		// TbEvent en=eventDAO.findById(eventID);
		// System.out.println("------eventID----haohao---"+eventID);
		// System.out.println("------user----haohao---"+user.getUId());
		// System.out.println("----------haohao--------"+tmp+"--------------------------");
		
		// 被上级同意的也显示
		String queryString0 = "select count(*) from TbPropose p where p.tbEvent.EId = ? and p.b = ? and p.c = '1'";
		long tmp0 = (Long) baseDAO.find(queryString0, obj).get(0);
		if (tmp0 == 1) {
			return true;
		}
		
		// 自己同意的自己不显示
		String queryString01 = "select count(*) from TbPropose p where p.tbEvent.EId = ? and p.a = ? and p.c = '1'";
		long tmp01 = (Long) baseDAO.find(queryString01, obj).get(0);
		if (tmp01 == 1) {
			return false;
		}
		

		// 只要添加过呈批意见的，都不可以再添加
		String queryString = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrId.UId = ?";
		long tmp = (Long) baseDAO.find(queryString, obj).get(0);
		if (tmp == 1) {
			return false;
		}
		//如果a字段不为空时，a字段内容与当前用户ID不相符不显示
		String qry="select count(*) from TbPropose p where p.a is not null and p.tbEvent.EId = ? and p.a != ?";
		long qrytmp = (Long) baseDAO.find(qry, obj).get(0);
		if (qrytmp==1) {
			return false;
		}
		
		/*
		 * 下级获得同意，正确代码 String que =
		 * "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrId.UId = ? and pr.c = '1'"
		 * ; long tmp1 = (Long) baseDAO.find(que, obj).get(0); if (tmp1==1) {
		 * return true; }
		 */
		// 同意过下级的也不显示
//		String que3 = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrCheckId.UId = ? and pr.c = '1'";
//		long tmp3 = (Long) baseDAO.find(que3, obj).get(0);
//		if (tmp3 == 1) {
//			return false;
//		}
		/*
		 * String que1 =
		 * "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrCheckId.UId = ? and pr.c = '2'"
		 * ; long tmp2 = (Long) baseDAO.find(que1, obj).get(0); if (tmp2==0) {
		 * return true; }
		 */
		return true;
	}

	/**
	 * 满足显示条件，返回TRUE 显示条件：无呈批、呈批经过上级审核 暂时不用
	 */
	@Override
	public boolean checkSubmit4Tag(HttpSession session, String eventID) {
		UserSession userSession = (UserSession) session
				.getAttribute(UserUtil.USER_SESSION);
		TbUser user = userSession.getTbUser();
		Object[] obj = { eventID, user.getUId() };

		/*
		 * 下级获得同意，正确代码 String que =
		 * "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrId.UId = ? and pr.c = '1'"
		 * ; long tmp1 = (Long) baseDAO.find(que, obj).get(0); if (tmp1==1) {
		 * return true; }
		 */
		// 被上级同意显示
		String que3 = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrId.UId = ? and pr.c = '1'";
		long tmp3 = (Long) baseDAO.find(que3, obj).get(0);
		if (tmp3 == 1) {
			return true;
		}
		// 不同意下级
		String que4 = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrCheckId.UId = ? and pr.c = '2'";
		long tmp4 = (Long) baseDAO.find(que4, obj).get(0);
		if (tmp4 == 1) {
			return true;
		}
		/*
		 * String que1 =
		 * "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrCheckId.UId = ? and pr.c = '2'"
		 * ; long tmp2 = (Long) baseDAO.find(que1, obj).get(0); if (tmp2==0) {
		 * return true; }
		 */
		// 只要添加过呈批意见的，都不可以再添加
		String queryString = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrId.UId = ?";
		long tmp = (Long) baseDAO.find(queryString, obj).get(0);
		String queryString1 = "select count(*) from TbProposeRecord pr join pr.tbPropose pp join pp.tbEvent et where et.EId = ? and pr.tbUserByPrCheckId.UId = ?";
		long tmp5 = (Long) baseDAO.find(queryString1, obj).get(0);
		if (tmp == 0 && tmp5 == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断“同意下级”按钮的显隐
	 */
	@Override
	public boolean agreeLower4Tag(HttpSession session, String eventID) {
		UserSession userSession = (UserSession) session
		.getAttribute(UserUtil.USER_SESSION);
		TbUser user = userSession.getTbUser();
		Object[] obj = { eventID, user.getUId() };
		String queryString = "select count(*) from TbPropose p where p.tbEvent.EId = ? and p.a = ? and p.c is null";
		long tmp = (Long) baseDAO.find(queryString, obj).get(0);
		if (tmp == 1) {
			return true;
		}
		return false;
		//return true;
	}
}
